
package com.chessimo.main;

import com.chessimo.util.Utilities;



/*
 * Notation class store the current Exercise (or Diagram) that is showing at the Board
 * It stores all information from the file (modules directory) in a format a little more understandable 
 */



public class Notation {
    
    
    private String[] tabPlys = new String[500];
    private static int indTabPlys;						 // Store the num of plys of current position
    private String[] tabComments = new String[500];
    private int indTabComments;
    private int[] posTabComments = new int[500]; // Store the position (move number) of the correspondent comment 
    private Utilities my = new Utilities();
    

    public void resetTabs() {
        
        for (int i=0; i < 500; i++) {
            tabPlys[i] = "";
            tabComments[i] = "";
            posTabComments[i] = 0;
        }        

        indTabComments = -1;
        indTabPlys = -1;        
        
    }
    
    public String getMove(int ind) {
        
        if (ind <= indTabPlys) {
            return tabPlys[ind];
        } else {
            return "";
        }
        
   
    }
    
    public static int getTotPlys () {
    	
    	return indTabPlys + 1;
    	
    }
    
    public String getComment(int ind) {
        
        for (int i=0; i <= indTabComments; i++) {
            if (ind == posTabComments[i]) {
                return tabComments[i];
            }
        }
        
        return "";
        
    }
    
    
    public String[] getPriorMoves(int ind) {
    
        String[] tabMoves = new String[ind];
        
        for (int i=0; i < ind; i++) {
            tabMoves[i] = tabPlys[i];
        }
        
        return tabMoves;
        
    }
     
     
    public void splitNotation(String txt) {
        
        int len = txt.length();
        int brackets = 0;
        String myString = "";

        resetTabs();
        
        for(int i=0; i < len; i++) {
            String thisChar = txt.substring(i, i+1);
            if (thisChar.equals("{") || thisChar.equals("[") || thisChar.equals("(")) {
                brackets++;
                indTabComments++;
                posTabComments[indTabComments] = indTabPlys;
            }
            
            if (thisChar.equals(" ")) {
                extractPlys(myString);
                myString = "";
            }
            
            if (brackets == 0) {
                myString = myString + thisChar;
            } else {
                tabComments[indTabComments] = tabComments[indTabComments] + thisChar;
            }
            if (thisChar.equals("}") || thisChar.equals("]") || thisChar.equals(")")) {
                brackets--;
            }            
        }
        

        
        
    }
    
    
    public void extractPlys(String txt) {
    
        txt = txt.trim();
        //String last = Chessimo_01.mySubstring(txt, txt.length()-1, 1);
        String first = my.mySubstring(txt, 0, 1);
        String possible = "OBNKQRabcdefgh";
        String thisChar;
        
        for (int i=0; i < possible.length(); i++) {
            thisChar = my.mySubstring(possible, i, 1);
            //System.out.println(thisChar + first);
            if (first.equals(thisChar)) {
                indTabPlys++;
                tabPlys[indTabPlys] = txt;
                return;
            }
        }
               
        
    }
    
    
    //FIXME: Only for Testes
    public void printComments() {
        
        for (int i=0; i <= indTabComments; i++) {
            System.out.println(tabComments[i] + "=" + posTabComments[i]);
        }
        
    }
    
  //FIXME: Only for Testes
    public void printPlys() {
        
        for (int i=0; i <= indTabPlys; i++) {
            System.out.println(tabPlys[i]);
        }        
        
        
    }
    
    
    public String getLangComment(String lang, String txt) {
        
        String temp = "";
        txt = txt.replace("}","");
        int pos = txt.indexOf("~~~" + lang + ":~");
        
        if (pos < 0) {
            return "";
        }
        
        pos = pos + 8;
        for (int i=pos; i < txt.length(); i++) {
            if (txt.charAt(i) == '~' ) {
                break;
            }
            temp = temp + txt.charAt(i);
        }
        
        return temp;
        
    }
    
    
    
    
    
    
    
    
    
}
